var reqparam=window.location.href.split("=")[1];
Ext.setup({
    icon:'icon.png',
    tabletStartupScreen:'tablet_startup.png',
    phoneStartupScreen:'phone_startup.png',
    glossOnIcon:false,
    onReady:function () {
        var form;
        
        Ext.regModel('DetailyData', {
            fields: [
                {name: 'ID',     type: 'string'},
                {name: 'DETAIL_ID', type: 'string'},
                {name: 'COSTDATE',    type: 'date'},
                {name: 'TYPE',      type: 'string'},
                {name: 'STARTPLACE',     type: 'string'},
                {name: 'ENDPLACE',     type: 'string'},
                {name: 'REASON',   type: 'string'},
                {name: 'DESCRIBE',    type: 'string'},
                {name: 'PRICE',     type: 'string'}
            ]
        });
        
        Ext.Ajax.request({
            url:'../../costmanagement/dailyListCost_saveDetailyCost.action?reqparam='+reqparam,
            success:function (response, opts) {
            	var res=JSON.parse(response.responseText).saveDetailyCost;
            	formBase.detailyData = Ext.ModelMgr.create({
                    'ID'    : res.ID,
                    'DETAIL_ID': res.DETAIL_ID,
                    'COSTDATE'   : res.COSTDATE,
                    'TYPE' : res.TYPE,
                    'STARTPLACE': res.STARTPLACE,
                    'ENDPLACE'  : res.ENDPLACE,
                    'REASON'    :res.REASON,
                    'DESCRIBE'    : res.DESCRIBE,
                    'PRICE'   : res.PRICE
                },  'DetailyData');
                form.loadModel(formBase.detailyData);
            }
        });
        
	   	Ext.regModel('costModel', {
	   	    fields: [
	   	        {name: 'CODE', type: 'string'},
	   	        {name: 'TRAFFICTYPE', type: 'string'}
	   	    ]
	   	});
	   	
        var costType = new Ext.data.Store({
			autoLoad: true,
		    model: 'costModel',
		    proxy: {
		        type: 'ajax',
		        url : '../../dic/dic_getCostType.action',
		        reader: {
		            type: 'json',
		        }
		    }
		});
        
        var formBase = {
            scroll:'vertical',
            standardSubmit:false,
            items:[
                {
                    xtype:'fieldset',
                    title:'报销信息',
                    defaults:{
                    	required:true,
                        xtype:'fieldset'
                    },
                    items:[
                           {
                               xtype:'textfield',
                               name:'ID',
                               label:'序号',
                               value:'',
                               required:false,
                               disabled:true,
                               useClearIcon:false
                           },
                           {
                               xtype:'selectfield',
                               name:'TYPE',
                               label:'费用类型',
                               placeHolder : '请选择费用类型',
       						   valueField: 'CODE',
       						   displayField:'TRAFFICTYPE',
       						   store : costType,
                               useClearIcon:false
                           },
                           {
                               xtype:'datepickerfield',
                               name:'COSTDATE',
                               label:'日期',
                               value:new Date(),
                               useClearIcon:false
                           },
                           {
                               xtype:'textfield',
                               name:'STARTPLACE',
                               label:'起始地',
                               value:'',
                               useClearIcon:false
                           },
                           {
                               xtype:'textfield',
                               name:'ENDPLACE',
                               label:'目的地',
                               value:'',
                               useClearIcon:false
                           },
                           {
                               xtype:'textfield',
                               name:'PRICE',
                               label:'金额',
                               value:'',
                               useClearIcon:false
                           },
                           {
                               xtype:'textareafield',
                               name:'DESCRIBE',
                               label:'费用描述',
                               maxLength:60,
                               maxRows:3
                           }
                    ]
                }
            ],
            listeners:{
                submit:function (form, result) {
                    console.log('success', Ext.toArray(arguments));
                },
                exception:function (form, result) {
                    console.log('failure', Ext.toArray(arguments));
                }
            },

            dockedItems:[
                {
                    xtype:'toolbar',
                    dock:'bottom',
                    items:[

                        {
                        	iconMask: true,
                        	iconCls: 'x-icon-mask trash',
                            handler:function () {
                            	Ext.Msg.confirm('系统提示','确定要删除？',function(btn){
                    		        if(btn=='yes'){
                    		        	Ext.Ajax.request({        	
                                            url:'../../costmanagement/dailyListCost_deleteDetail.action?reqparam='+reqparam,
                                            success:function (response, opts) {
                                            	Ext.Msg.alert('提示', '删除成功',function(btn){
                            						window.location.href='../../costmanagement/dailyCostSubmit/dailyCostList.jsp?';
                            					});	
                                            }
                                        });
                    		        }else{
                    		        	return;
                    		        }
                            		        
                            	},this);
                            }
                        },
                        {
                            xtype:'spacer'
                        },
                        {
                            text:'重置',
                            handler:function () {
                                form.reset();
                            }
                        },
                        {
                            text:'保存',
                            ui:'confirm',
                            handler:function () {
                            	var detailcode=reqparam.split(",")[4]
                            	Ext.Msg.confirm('系统提示','确定要提交？',function(btn){
                    		        if(btn=='yes'){
                    		          if(detailcode==null || detailcode.length<1){
              								var url="../../costmanagement/dailyListCost_detailInsert.action?reqparam="+reqparam;
              								formBase.submitform(url);
                                  		}else{
              								var url="../../costmanagement/dailyListCost_detailUpdate.action?reqparam="+reqparam;
              								formBase.submitform(url);
                                  		}
                    		        }else{
                    		        	return;
                    		        }
                            		        
                            	},this);
                            }
                        }
                    ]
                }
            ],
            
            submitform:function(url){
            	form.submit( {
    				waitMsg : {
    					message : '正在提交...',
    					cls : 'demos-loading'
    				},
    			 url: url,           
    			 method: 'POST',           
    			 success: function(thisForm, action){ 
    					Ext.Msg.alert('提示', '提交成功',function(btn){
    					//	window.location.href='../../costmanagement/dailyCostSubmit/dailyCostList.jsp';
    					});	
    					
    				}
    			});
            }
        };

        if (Ext.is.Phone) {
            formBase.fullscreen = true;
        } else {
            Ext.apply(formBase, {
                autoRender:true,
                floating:true,
                modal:true,
                centered:true,
                hideOnMaskTap:false,
                height:385,
                width:480
            });
        }

        form = new Ext.form.FormPanel(formBase);
        form.show();
    }
});